﻿/*
 * Created by SharpDevelop.
 * User: XPMUser
 * Date: 11/01/2011
 * Time: 17:47
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Windows.Input;

namespace KDG.FoldersSize4
{
	/// <summary>
	/// Description of MainViewModel.
	/// </summary>
	public class MainViewModel : ViewModelBase
	{
		private MainModel _model = null;
		private ViewModelBase _scanViewModel = null;
		private ViewModelBase _findViewModel = null;
		private ViewModelBase _saveViewModel = null;
		
		public MainViewModel() {}
		public MainViewModel(MainModel Model)
		{
			_model= Model;
			
			_scanViewModel = new ScanViewModel(_model.Model);
			(_scanViewModel as ScanViewModel).OpenInNewWindowEvent += new EventHandler(MainViewModel_OpenInNewWindowEvent);
			_findViewModel = new FindViewModel(_model.Model);
			_saveViewModel = new SaveViewModel(_model.Model);
			
			_model.CurrentView = _scanViewModel;
			
		}

		void MainViewModel_OpenInNewWindowEvent(object sender, EventArgs e)
		{
			ScanViewModel svm = sender as ScanViewModel;
			if (svm != null && svm.SelectedItem!=null)
			{
				MainWindow mainWindow = new MainWindow();
				FoldersSizeModel fsModel = new FoldersSizeModel();
				fsModel.RootItem = svm.SelectedItem.Clone();
				fsModel.RootItem.CalcPprogressTotal(fsModel.RootItem.Length);
				fsModel.RootPath = svm.SelectedItem.FullName;
				
				MainViewModel mainViewModel = new MainViewModel( new MainModel() {  Model = fsModel });
			 	mainWindow.DataContext = mainViewModel;
					
				mainWindow.Show();
			}
			
				
			
		}
		
		public MainModel MainModel
		{
			get { return _model; }
		}
		public bool IsScanning
		{
			get { return _model.Model.IsScanning; }
			set {
				if(_model.Model.IsScanning==value)
					return ;
				
				_model.Model.IsScanning=value;
				OnPropertyChanged("IsScanning");
			}
		}
		
		bool CanCangeView()
		{
			return !(IsScanning || _model.Model.RootItem==null);
		}
		
		public ViewModelBase CurrentView
		{
			get { return _model.CurrentView; }
			set {
				if(_model.CurrentView==value)
					return;
				_model.CurrentView=value;
				
				OnPropertyChanged("MainModel");
			}
		}
		private RelayCommand _scanViewCommand = null;
		private RelayCommand _findViewCommand = null;
		private RelayCommand _saveViewCommand = null;
		
		public ICommand ScanViewCommand
		{
			get {
				if(_scanViewCommand == null)
					_scanViewCommand = new RelayCommand( p=>
					                                    {
					                                    	this.CurrentView = _scanViewModel;
					                                    }
					                                   );
				
				return _scanViewCommand;
			}
		}
		
		
		
		public ICommand FindViewCommand
		{
			get {
				if(_findViewCommand == null)
					_findViewCommand = new RelayCommand( p=>
					                                    {
					                                    	this.CurrentView = _findViewModel;
					                                    },
					                                    c => this.CanCangeView()
					                                   );
				
				return _findViewCommand;
			}
		}
		public ICommand SaveViewCommand
		{
			get {
				if(_saveViewCommand == null)
					_saveViewCommand = new RelayCommand( p=>
					                                    {
					                                    	this.CurrentView = _saveViewModel;
					                                    },
					                                    c => this.CanCangeView()
					                                   );
				
				return _saveViewCommand;
			}
		}
	}
}
